/** 
*@项目名称: Pear-BaseUI 
*@文件名称: WebViewActivity.java 
*@Author: linlin.zou
*@Date: 2015-3-18 
*@Copyright: 2015 www.boshtc.com Inc. All rights reserved.

*注意：本内容仅限于上海鸭梨网络有限公司内部传阅，禁止外泄以及用于其他的商业目的 
*/ 
package com.alipear.uibase;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class BaseWebViewActivity extends BaseActivity {
	private WebView web;

	public void initWebView(WebView webView) {
		this.web = webView;
		web.getSettings().setJavaScriptEnabled(true);
		web.setWebViewClient(new HelloWebViewClient());

	}

	public boolean loadUrl(String url) {
		if (url != null) {
			web.loadUrl(url);
		} else {
			return false;
		}
		return true;
	}

	public WebView getWebView() {
		return web;
	}

	// Web视图
	private class HelloWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			showProgressDialog();
		}

		/**
		 * Notify the host application that a page has finished loading. This
		 * method is called only for main frame. When onPageFinished() is
		 * called, the rendering picture may not be updated yet. To get the
		 * notification for the new Picture, use
		 * {@link WebView.PictureListener#onNewPicture}.
		 * 
		 * @param view
		 *            The WebView that is initiating the callback.
		 * @param url
		 *            The url of the page.
		 */
		@Override
		public void onPageFinished(WebView view, String url) {
			closeProgressDailog();
		}
	}

	@Override
	// 设置回退
	// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack(); // goBack()表示返回WebView的上一页面
			return true;
		} else {
			this.finish();
		}
		return false;
	}
}
